import { Component,OnInit,Output,EventEmitter ,Input} from '@angular/core';
import {appService} from "../../../../../my-service/app.service";
import {UtilService} from "../../../../../my-service/util.service";

@Component({
  selector: 'recovery-root',
  templateUrl: './recovery.component.html',
  styleUrls: ['./recovery.component.css']
})
export class recoveryComponent implements OnInit{
  FK_CanvasGuid
  img
  constructor(private appservice: appService, private utilService: UtilService) {
    this.FK_CanvasGuid=this.utilService.canvas().guid
    this.img=this.utilService.canvas().img
  }
  @Input() classguid: any;
  @Output() onhuifu = new EventEmitter<Event>();
  title = 'recovery';
  FK_CreatorGuid;
  trainlist=[];
  load=false;
  nzTotal;
  nzPageIndex=1;
  trainlistshow=[];
  nzPageSize=8;
  ngOnInit() {
    this.utilService.getUserInfo().then(re=>{
      this.FK_CreatorGuid=re.userGuid
    this.appservice.lpplist({
      FK_CanvasGuid:this.FK_CanvasGuid,
      FK_CreatorGuid:this.FK_CreatorGuid,
      FK_ClassGuid:this.classguid,
      Status:0
    }).then((re2)=>{
      if(re2.length>0){
        this.trainlist=re2;
        this.nzTotal=re2.length;
        let len:number;
        if( this.nzTotal>this.nzPageSize) len=this.nzPageSize
        else if (this.nzTotal<=this.nzPageSize) len=this.nzTotal
        for(let i=0;i<len;i++){
          this.trainlistshow.push(re2[i])
        }
      }else{
        this.trainlist=[]
        this.trainlistshow=[];
      }
    }).then(()=>{
      this.load=true;
    })
  })
  }
  pagin(){
    this.trainlistshow = []
    let n = (this.nzPageIndex-1) * this.nzPageSize;
    for (let i = 0; i < this.nzPageSize; i++) {
      if ((n + i) < this.trainlist.length) {
        this.trainlistshow.push(this.trainlist[i + n])
      }
    }
  }
  huifu(item,i){
    this.utilService.comfirm('确定要恢复该训练吗？',()=>{
      this.appservice.deltrain({
        PK_TrainningGuid:item.PK_TrainningGuid,
        Status:1
      }).then(re=>{
        if(re.status==1){
          for(let j=0;j<this.trainlist.length;j++){
            if(this.trainlist[j].PK_TrainningGuid==item.PK_TrainningGuid){
              this.trainlist.splice(j, 1);
            }
          }
          this.pagin()
          this.onhuifu.emit(item)
          this.utilService.remind('success','恢复成功！')
        }
      })
    })
  }
}
